package com.linghang.back.controller;

import com.linghang.back.dto.*;
import com.linghang.back.service.UserService;
import com.linghang.back.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户注册、登录、信息管理等接口")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @PostMapping("/verify-code")
    @Operation(summary = "发送验证码", description = "向指定手机号发送验证码")
    public ApiResponse<String> sendVerifyCode(@Valid @RequestBody VerifyCodeRequest request) {
        String verifyCode = userService.sendVerifyCode(request);
        return ApiResponse.success("验证码发送成功", verifyCode);
    }
    
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "通过手机号、验证码、用户名、密码注册新用户")
    public ApiResponse<UserResponse> register(@Valid @RequestBody RegisterRequest request) {
        UserResponse user = userService.register(request);
        return ApiResponse.success("注册成功", user);
    }
    
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "使用手机号和验证码登录，如用户未注册则自动注册。邀请码为非必填项，仅在新用户自动注册时生效。")
    public ApiResponse<UserResponse> login(@Valid @RequestBody LoginRequest request) {
        UserResponse user = userService.login(request);
        return ApiResponse.success("登录成功", user);
    }
    
    @GetMapping("/info")
    @Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
    public ApiResponse<UserResponse> getUserInfo(@RequestHeader("Authorization") String token) {
        String jwtToken = token.replace("Bearer ", "");
        String userIdStr = jwtUtil.getUserIdFromToken(jwtToken);
        Long userId = Long.parseLong(userIdStr);
        UserResponse user = userService.getUserInfo(userId);
        return ApiResponse.success(user);
    }
    
    @PostMapping("/invite-code")
    @Operation(summary = "生成邀请码", description = "为当前用户生成邀请码")
    public ApiResponse<String> generateInviteCode(@RequestHeader("Authorization") String token) {
        String jwtToken = token.replace("Bearer ", "");
        String userIdStr = jwtUtil.getUserIdFromToken(jwtToken);
        Long userId = Long.parseLong(userIdStr);
        String inviteCode = userService.generateInviteCode(userId);
        return ApiResponse.success("邀请码生成成功", inviteCode);
    }
    
    @PostMapping("/bind-inviter")
    @Operation(summary = "绑定邀请关系", description = "使用邀请码绑定邀请关系")
    public ApiResponse<Boolean> bindInviter(
            @RequestHeader("Authorization") String token,
            @RequestParam String inviteCode) {
        String jwtToken = token.replace("Bearer ", "");
        String userIdStr = jwtUtil.getUserIdFromToken(jwtToken);
        Long userId = Long.parseLong(userIdStr);
        boolean success = userService.bindInviter(userId, inviteCode);
        return ApiResponse.success("绑定邀请关系成功", success);
    }
} 